package com.tanhua.server.service;


import cn.hutool.crypto.digest.DigestUtil;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.autoconfig.template.SmsTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.model.domain.User;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.server.exception.BusinessException;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {


    Log log = LogFactory.getLog(getClass());

    @Autowired
    SmsTemplate smsTemplate;

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @DubboReference
    UserApi userApi;

    @Autowired
    HuanXinTemplate huanXinTemplate;


    public void sendMsg(String phone) {
        String code = RandomStringUtils.randomNumeric(6);
//        smsTemplate.sendSms(phone, code);
        //redis
        redisTemplate.opsForValue().set("CHECK_CODE_" + phone, "123456", Duration.ofMinutes(7*24*60));

        String temp = redisTemplate.opsForValue().get("CHECK_CODE_" + phone);
        log.debug("验证码"+temp+"已经保存到redis");
    }

    public Map loginVerification(String phone, String code) {
        String redisCode = redisTemplate.opsForValue().get("CHECK_CODE_" + phone);
        log.debug("redis验证码:"+redisCode+"  输入验证码："+code);
        if (!code.equals(redisCode)) {
            throw new BusinessException(ErrorResult.loginError());
        }
        redisTemplate.delete("CHECK_CODE_" + phone);
        User user = userApi.findByMobile(phone);
        boolean isNew = false;
        if (user == null) {
            user = new User();
            user.setMobile(phone);
//            user.setCreated(new Date());
//            user.setUpdated(new Date());
            user.setPassword(DigestUtil.md5Hex("123456"));
            Long userId = userApi.save(user);
            user.setId(userId);
            isNew = true;

            //注册环信用户
            String hxUser = "hx"+user.getId();
            Boolean create = huanXinTemplate.createUser(hxUser, Constants.INIT_PASSWORD);
            if(create) {
                user.setHxUser(hxUser);
                user.setHxPassword(Constants.INIT_PASSWORD);
                userApi.update(user);
            }

        }
        //利用jwt生成token
        Map tokenMap = new HashMap();
        tokenMap.put("id", user.getId());
        tokenMap.put("mobile", phone);
        String token = JwtUtils.getToken(tokenMap);
        Map retMap = new HashMap();
        retMap.put("token", token);
        retMap.put("isNew", isNew);

        return retMap;
    }
}
